import React, { Component } from 'react'
import { Card, message, Button, Table, Modal, Tag } from 'antd'
import './myResume.less'
import { reqResumeList, reqResumeDelete } from '../../../../api'
import { to } from '../../../../utils/index'
import { Link } from 'react-router-dom'
const { confirm } = Modal

export default class MyResume extends Component {
    state = {
        current: 1,
        size: 12,
        total: 0,
        loading: false,
        dataSource: []
    }

    async componentDidMount() {
        this.getResumeList()
    }

    getResumeList = async () => {
        this.setState({ loading: true })
        let [err, res] = await to(reqResumeList(this.state.current, this.state.size))
        this.setState({ loading: false })
        if (err) return console.log(err)
        if (res.code !== 0) return message.error(res.msg)

        let resumeList = res.body.list
        this.setState({
            dataSource: resumeList,
            total: res.body.total
        })
    }

    handleTableChange = (pagination) => {
        this.setState({
            current: pagination.current
        }, () => {
            this.getResumeList()
        })
    }

    delete = (_id) => {
        let that = this
        confirm({
            title: '提示?',
            content: '确定要删除吗?',
            async onOk() {
                let [err, res] = await to(reqResumeDelete(_id))
                if (err) return console.log(err)
                if (res.code !== 0) return message.error(res.msg)
                message.success(res.msg)
                that.getResumeList()
            },
            onCancel() {
            },
        });

    }

    render() {
        const { dataSource, total, size, loading } = this.state
        const columns = [
            { title: '简历名称', dataIndex: 'name', key: 'name'},
            { title: '创建时间', dataIndex: 'ctime', key: 'ctime'},
            { title: 'word地址', dataIndex: 'wordUrl', key: 'wordUrl'},
            { title: 'pdf地址', dataIndex: 'pdfUrl', key: 'pdfUrl'},
            { title: '状态', dataIndex: 'status', key: 'status', render: text => <Tag color={text === 1 ? 'green' : 'red'}>{text === 1 ? '已发布' : '未发布'}</Tag>},
            {
                title: '操作', dataIndex: '', key: 'cz', render: (text) => {
                    return (
                        <span>
                            <Link to={"/resume/addresume/add/" + text._id}>
                                <Button type="link">编辑</Button>
                            </Link>
                            |
                            <Button disabled={text.status === 0} type="link" target="_blank" href={'#resumeTemplate?_id=' + text._id}>打开简历</Button>
                            |
                            <Button type="link" onClick={() => this.delete(text._id)}>删除</Button>
                        </span>
                    )
                }
            },
        ]

        return (
            <Card title="我的简历" style={{height: '100%'}}>
                <Table
                    dataSource={dataSource}
                    columns={columns}
                    rowKey='_id'
                    pagination={{ total, defaultPageSize: size }}
                    loading={loading}
                    onChange={this.handleTableChange}
                />
            </Card>
        )
    }
}
